package com.game;

import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import android.view.MotionEvent;

public class Scene1 extends Scene {

	private final int Continue=0, NewGame=1, TimeTrial=2, Options=3;
	
	private Bitmap m_Background;
	private Bitmap m_Title;
	
	private Vector<Button> m_Buttons;
	
	private boolean m_bPaused;
	private AlertBox m_AlertBox;
	
	private boolean m_bInfo;
	private InfoBox m_InfoBox;
	private ButtonOneTouch m_InfoButton;
	
	public Scene1(SceneManager parent)
	{
		super(parent);
		m_Background = LoadBitmap(R.drawable.scene1_bg);		
		m_Title = LoadBitmap(R.drawable.scene1_title);
		
		m_Buttons = new Vector<Button>();
		
		Button b1;
		b1= new Button(Continue, 151, 140, LoadBitmap(R.drawable.scene1_continue_on), LoadBitmap(R.drawable.scene1_continue_off));
		m_Buttons.add(b1);
		
		if(!mParentSM.GotSavedGame()) { b1.Disable(); }
		
		b1= new Button(NewGame, 151, 180, LoadBitmap(R.drawable.scene1_newgame_on), LoadBitmap(R.drawable.scene1_newgame_off));
		m_Buttons.add(b1);
		
		b1= new Button(TimeTrial, 151, 220, LoadBitmap(R.drawable.scene1_timetrial_on), LoadBitmap(R.drawable.scene1_timetrial_off));
		m_Buttons.add(b1);
		
		b1= new Button(Options, 151, 260, LoadBitmap(R.drawable.scene1_options_on), LoadBitmap(R.drawable.scene1_options_off));
		m_Buttons.add(b1);
		
		
		
		Bitmap opacityScreen = LoadBitmap(R.drawable.general_opacity);
		
		// The pop-up screen
		m_bPaused = false;	
		Button yes = new Button(0, 100, 175, LoadBitmap(R.drawable.scene2_yes_on), LoadBitmap(R.drawable.scene2_off));
		Button no = new Button(1, 270, 175, LoadBitmap(R.drawable.scene2_no_on), LoadBitmap(R.drawable.scene2_off));
		m_AlertBox = new AlertBox(	opacityScreen , LoadBitmap(R.drawable.scene2_popup), yes, no);

		// The info scene
		m_bInfo = false;
		ButtonOneTouch infoBoxButton = new ButtonOneTouch(0, 90, 50, LoadBitmap(R.drawable.scene1_info_popup));
		m_InfoBox = new InfoBox(opacityScreen, infoBoxButton);
		
		m_InfoButton = new ButtonOneTouch(0,420,290, LoadBitmap(R.drawable.scene1_info_button));
	}
	
	@Override
	public void Draw(Canvas canvas) 
	{		
		canvas.drawBitmap(m_Background, 0, 0, null);
		canvas.drawBitmap(m_Title, 40, 10, null);
		
		for (Button button : m_Buttons) {
			button.Draw(canvas);
		}
		
		m_InfoButton.Draw(canvas);
		
		if(m_bPaused)
		{
			m_AlertBox.Draw(canvas);
		}
		
		if(m_bInfo)
		{
			m_InfoBox.Draw(canvas);
		}
	}

	@Override
	public void End() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Start() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Update() {
		for (Button button : m_Buttons) 
		{
			if(button.Used())
			{
				switch (button.getID()) 
				{
					case Continue:	ClickContinue();	button.Unselect();	break;
					case NewGame:	ClickNewGame();		button.Unselect();	break;
					case TimeTrial:	ClickTimeTrial();	button.Unselect();	break;
					case Options:	ClickOptions();		button.Unselect();	break;	
					default:												break;
				}
			}
		}
		if(m_InfoButton.Used())
		{
			m_bInfo = true;
			m_InfoButton.Unselect();
		}

	}

	@Override
	public boolean handleTouch(MotionEvent event)
	{
		synchronized (mSurfaceHolder) 
		{
			int tx,ty;
			tx = (int)event.getX();
			ty = (int)event.getY();
			
			if(m_bPaused)
			{
				int res = m_AlertBox.HandleTouch(tx, ty);
				if(res == 0) m_bPaused = false;
				if(res == 1) mParentSM.ChangeScene(new Scene6(mParentSM));
			}
			else if (m_bInfo)
			{
				if(m_InfoBox.HandleTouch(tx, ty)) m_bInfo = false;
			}
			else // default behavior
			{
				for (Button button : m_Buttons) 
				{
					button.TryTouch(tx, ty);				
				}
				m_InfoButton.TryTouch(tx, ty);
			}
			
			
			
			
		}
		return true;
	}
	
	private void ClickContinue()
	{
		mParentSM.ChangeScene(new Scene3(mParentSM));
	}
	
	private void ClickNewGame()
	{
		if(mParentSM.GotSavedGame())
		{
			m_bPaused = true;
		}
		else
		{
			mParentSM.ChangeScene(new Scene6(mParentSM));
		}
	}
	private void ClickTimeTrial()
	{
		
	}
	
	private void ClickOptions()
	{
		mParentSM.ChangeScene(new Scene7(mParentSM));
	}

}
